package com.yy.rabbitmq.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class MessageConfig {

    /**
     * 邮件队列
     */
    public static final String SMS_EMAIL_QUEUE = "SMS_EMAIL_QUEUE";

    /**
     * 短信队列
     */
    public static final String SMS_PHONE_QUEUE = "SMS_PHONE_QUEUE";

    /**
     * 日志队列，需要同时收集email和phone
     */
    public static final String SMS_LOG_QUEUE = "SMS_LOG_QUEUE";

    /**
     * 消息交换机
     */
    public static final String SMS_TOPIC_EXCHANGE = "SMS_TOPIC_EXCHANGE";


    /**
     * 死信交换机
     */
    public static final String SMS_DEAD_EXCHANHE = "SMS_DEAD_EXCHANHE";
    /**
     * 死信队列
     */
    public static final String SMS_DEAD_QUEUE = "SMS_DEAD_QUEUE";

    /**
     * 死信routingKey
     */
    public static final String SMS_DEAD_ROUTINGKEY = "SMS_DEAD_ROUTINGKEY";

    @Bean
    public Queue smsEmailQueue() {
        Map<String, Object> args = new HashMap<>(2);
        // x-dead-letter-exchange    这里声明当前队列绑定的死信交换机
        args.put("x-dead-letter-exchange", SMS_DEAD_EXCHANHE);
        // x-dead-letter-routing-key  这里声明当前队列的死信路由key
        args.put("x-dead-letter-routing-key", SMS_DEAD_ROUTINGKEY);
        args.put("x-message-ttl", 2000);
        return new Queue(SMS_EMAIL_QUEUE,true,false,false,args);
    }


    @Bean
    public Queue deadEmailQueue() {
        return new Queue(SMS_DEAD_QUEUE);
    }

    @Bean
    public Queue smsPhoneQueue() {
        return new Queue(SMS_PHONE_QUEUE);
    }

    @Bean
    public Queue smsLogQueue() {
        return new Queue(SMS_LOG_QUEUE);
    }


    @Bean
    public TopicExchange smsTopicExchange() {
        return new TopicExchange(SMS_TOPIC_EXCHANGE);
    }

    // 声明死信Exchange
    @Bean
    public TopicExchange smsDeadExchange() {
        return new TopicExchange(SMS_DEAD_EXCHANHE);
    }

    @Bean
    public Binding bindingSmsExchangToSmsEmailQueue(Queue smsEmailQueue, TopicExchange smsTopicExchange) {
        return BindingBuilder.bind(smsEmailQueue).to(smsTopicExchange).with("sms.email");
    }

    @Bean
    public Binding bindingSmsExchangToSmsPhoneQueue(Queue smsPhoneQueue, TopicExchange smsTopicExchange) {
        return BindingBuilder.bind(smsPhoneQueue).to(smsTopicExchange).with("sms.phone");
    }

    @Bean
    public Binding bingSmsExchangToSmsLogQueue(Queue smsLogQueue, TopicExchange smsTopicExchange) {
        return BindingBuilder.bind(smsLogQueue).to(smsTopicExchange).with("sms.*");
    }

    @Bean
    public Binding deadLetterBinding(Queue deadEmailQueue,
                                     TopicExchange smsDeadExchange) {
        return BindingBuilder.bind(deadEmailQueue).to(smsDeadExchange).with(SMS_DEAD_ROUTINGKEY);
    }
}
